﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace Meechisoft.TFS.BranchIndicator
{
    using Microsoft.TeamFoundation.Controls;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Framework.Client;
    using Microsoft.TeamFoundation.VersionControl.Client;

    using System.Windows.Media;
    using System.Windows.Threading;

    [TeamExplorerSection(BranchSettingsSection.SectionId, TeamExplorerPageIds.Settings, 40)]
    public sealed class BranchSettingsSection : TeamExplorerBaseSection
    {
        public const string SectionId = "F4226B28-5460-4ED3-84C2-851285F89DB9";

        private readonly BranchSettingsModel Model;
        private readonly BranchSettingsView View;

        public BranchSettingsSection()
        {
            this.Title = "Branch Settings";

            this.Model = new BranchSettingsModel();
            this.View = new BranchSettingsView();
            this.Model.Branches = new ObservableCollection<BranchSetting>();
            this.View.DataContext = Model;
            this.SectionContent = View;

        }

        public void ShowError(string message)
        {
            this.ShowNotification(message, NotificationType.Error);
        }

        public override void Refresh()
        {
            base.Refresh();

            LoadSettings(this.CurrentContext);
        }

        public override void Loaded(object sender, SectionLoadedEventArgs e)
        {
            base.Loaded(sender, e);

            LoadSettings(this.CurrentContext);
        }

        protected override void ContextChanged(object sender, ContextChangedEventArgs e)
        {
            base.ContextChanged(sender, e);

            LoadSettings(e.NewContext);
        }

        public override void SaveContext(object sender, SectionSaveContextEventArgs e)
        {
            base.SaveContext(sender, e);

            Task.Factory.StartNew(DoSave, this.CurrentContext);
        }

        private void DoSave(object obj)
        {
            var context = obj as ITeamFoundationContext;

            if (context== null)
                return;

            var registry = context.TeamProjectCollection.ConfigurationServer.GetService<ITeamFoundationRegistry>();

            foreach (var br in Model.Branches)
            {
                registry.SetBranchColours(br);
            }

            //NOTE: Is there some way to get a refrence to the package?
            if (BranchIndicatorPackage.Instance != null)
                BranchIndicatorPackage.Instance.CheckForSolutionInBranch();

        }

        private void LoadSettings(ITeamFoundationContext context)
        {
            Model.Branches.Clear();

            var vcs = context.TeamProjectCollection.GetService<VersionControlServer>();

            var branches = TfsHelpers.GetBranches(vcs);

            var registry = context.TeamProjectCollection.ConfigurationServer.GetService<ITeamFoundationRegistry>();

            foreach (var br in branches)
            {
                var setting = new BranchSetting() { ID = br.Properties.RootItem.Item.GetHashCode().ToString() };

                setting.Name = br.Properties.RootItem.Item;

                registry.PopulateBranchColours(setting);

                Model.Branches.Add(setting);

                setting.PropertyChanged += setting_PropertyChanged;
            }
        }

        private void setting_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            SaveContext(this, new SectionSaveContextEventArgs());
        }


    }
}
